import type { LoginResult } from '@/types/member'
import { http } from '@/utils/http'

let FixApi = ''
if (process.env.NODE_ENV == 'development') {
  FixApi = '/web' //开发环境
} else {
  FixApi = '/web' //生产环境
}

type LoginParams = {
  loginName : string
  passwd : string
}
/**
 * 传统登录-用户名+密码
 * @param data 请求参数
 */
export const postLoginAPI = (data : LoginParams) => {
  return http<LoginResult>({
    method: 'POST',
    url: `${FixApi}/ml/mlogin/loginIn.json?loginName=${data.loginName}&passwd=${data.passwd}`,
  })
}
/* 退出登录 */
export const postLogoutAPI = () => {
  return http({
    method: 'POST',
    url: `${FixApi}/ml/mlogin/loginOut.json`,
  })
}

/* 获取用户信息 */
export const GetUserInfoApi = () => {
  return http({
    method: 'GET',
    url: `${FixApi}/ml/user/mp/loginUserInfo`,
  })
}

/**
 * 保存设备信息
 */
export const saveClDevice = (data : any) => {
  return http({
    method: 'POST',
    url: `${FixApi}/cl/device/saveClDevice.json`,
    data

  })
}

/**
 * 校验当前设备有没有绑定
 */
export const checkDevice = (data : any) => {
  return http({
    method: 'POST',
    url: `${FixApi}/cl/device/queryClDevicePage.json`,
    data

  })
}

/**
 * 修改密码
 */
export const updatePsw = (data : any) => {
  return http({
    method: 'POST',
    url: `${FixApi}/um/huidaUserservice/updatePsw.json`,
    data
  })
}
//获取小程序token
export const getMiniprogramToken = () => {
  return http({
    url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxae7c253d16a2bf22&secret=c88a5a6f630b5244244aed8ebf324b54`,
    method: 'GET'
  })
}

const appSecret = '85fba4e2d45a1987c1ab9e7e5d19aa03'
const appId = 'wx175ab95db5361a27'
//获取小程序openid
export const getMiniprogramOpenId = (code : string) => {
  return http({
    url: `https://api.weixin.qq.com/sns/jscode2session?appid=wx78df1bc4eb51b8e8&secret=f8b0c7e3f0b65437e6faff2eb994cf9b&grant_type=authorization_code&js_code=${code}`,
    method: 'GET'
  })
}
//公众号的 tokenr
export const getMpToken = () => {
  return http({
    url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appId}&secret=${appSecret}`,
    method: 'GET'
  })
}
//获取所有用户openid
export const getopenIds = (assetToken : string) => {
  return http({
    url: `https://api.weixin.qq.com/cgi-bin/user/get?access_token=${assetToken}&next_openid=`,
    method: 'GET'
  })
}
//获取所有用户 详情
export const getUsers = (assetToken : string, data : any) => {
  return http({
    url: `https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=${assetToken}`,
    method: 'POST',
    data
  })
}

export const getOpenId = (code : string) => {
  return http({
    url: `${FixApi}/cl/device/getOpenid.json`,
    method: 'POST',
    data: {
      code
    }
  })
}
